package com.cskaoyan.service.goods;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.model.bo.goods.categorybrand.CategoryAndBrandRoot;
import com.cskaoyan.model.bo.goods.categorybrand.CategoryListChildren;
import com.cskaoyan.model.bo.goods.categorybrand.GoodsBrandList;
import com.cskaoyan.model.bo.goods.categorybrand.GoodsCategoryList;
import com.cskaoyan.model.bo.goods.create.*;
import com.cskaoyan.model.bo.goods.delete.GoodsDeleteBO;
import com.cskaoyan.model.bo.goods.list.GoodsListPageBo;
import com.cskaoyan.model.bo.goods.update.GoodsUpdateRoot;
import com.cskaoyan.model.vo.goods.GoodsBaseData;
import com.cskaoyan.model.vo.goods.detail.GoodsDetailData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: project-market
 * @description: 全部商品查询mapper
 * @author: Jesson Guo
 * @create: 2021-11-29 20:51
 **/
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    MallGoodsMapper goodsMapper;
    @Autowired
    MallBrandMapper brandMapper;
    @Autowired
    MallCategoryMapper categoryMapper;
    @Autowired
    MallGoodsSpecificationMapper specificationMapper;
    @Autowired
    MallGoodsProductMapper productMapper;
    @Autowired
    MallGoodsAttributeMapper attributeMapper;

    //查询全部商品
    @Override
    public GoodsBaseData allGoodsList(GoodsListPageBo pageBo) {
        //创建分页
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        //筛选
        MallGoodsExample mallGoodsExample = new MallGoodsExample();
        //排序

        MallGoodsExample.Criteria goodsExampleCriteria = mallGoodsExample.createCriteria();
        //筛选不是被删除的
        goodsExampleCriteria.andDeletedEqualTo(false);
        //商品id精确查询
        if (pageBo.getGoodsId() != null) goodsExampleCriteria.andIdEqualTo(pageBo.getGoodsId());
        if (pageBo.getGoodsSn() != null && !pageBo.getGoodsSn().equals(""))
            goodsExampleCriteria.andGoodsSnEqualTo(pageBo.getGoodsSn());
        //商品名模糊查询
        if (pageBo.getName() != null && !pageBo.getName().equals(""))
            goodsExampleCriteria.andNameLike("%" + pageBo.getName() + "%");
        mallGoodsExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        List<MallGoods> goods = goodsMapper.selectByExample(mallGoodsExample);
        PageInfo pageInfo = new PageInfo(goods);
        Integer total = Math.toIntExact(pageInfo.getTotal());
        return new GoodsBaseData(pageBo.getPage(), pageBo.getLimit(), total, goods);
    }

    //新增商品模块 查询品牌种类信息回显
    @Override
    public CategoryAndBrandRoot catAndBrand() {
        //brand
        List<MallBrand> mallBrands = brandMapper.selectByExample(null);
        List<GoodsBrandList> goodsBrandList = new ArrayList<>();
        for (MallBrand mallBrand : mallBrands) {
            goodsBrandList.add(new GoodsBrandList(mallBrand.getId(), mallBrand.getName()));
        }

        //第一次查pid为0的
        List<GoodsCategoryList> categoryList = categoryMapper.selectCategory();
        //第二次查categorylist下的子list
        List<CategoryListChildren> listChildren = new ArrayList<>();
        for (GoodsCategoryList goodsCategoryList : categoryList) {
            listChildren = categoryMapper.selectChildrenCategroy((int) goodsCategoryList.getValue());
            goodsCategoryList.setChildren(listChildren);
        }
        CategoryAndBrandRoot categoryAndBrandRoot = new CategoryAndBrandRoot();
        categoryAndBrandRoot.setBrandList(goodsBrandList);
        categoryAndBrandRoot.setCategoryList(categoryList);
        return categoryAndBrandRoot;
    }

    //新增商品
    @Override
    public int createNewGood(GoodsCreateRootBO goodsCreateRootBO) {
        //查重
        if (goodsMapper.selectSameName(goodsCreateRootBO.getGoods().getName()) != 0) {
            return 211;
        }

        //把ROOT四部分信息新增到所对应表

        try {
            List<GoodsCreateProducts> products = goodsCreateRootBO.getProducts();
            //part1商品表新增商品信息
            MallGoods mallGoods = new MallGoods();
            GoodsCreateGoodsBO goods = goodsCreateRootBO.getGoods();
            mallGoods.setGoodsSn(goods.getGoodsSn());
            mallGoods.setName(goods.getName());
            mallGoods.setCategoryId((int) goods.getCategoryId());
            mallGoods.setBrandId((int) goods.getBrandId());
            String[] galleryTemp = new String[goods.getGallery().size()];
            mallGoods.setGallery(goods.getGallery().toArray(galleryTemp));
            mallGoods.setKeywords(goods.getKeywords());
            mallGoods.setBrief(goods.getBrief());
            mallGoods.setIsOnSale(goods.isOnSale());
            mallGoods.setPicUrl(goods.getPicUrl());
            mallGoods.setIsNew(goods.isNew());
            mallGoods.setIsHot(goods.isHot());
            mallGoods.setUnit(goods.getUnit());
            mallGoods.setCounterPrice(goods.getCounterPrice());
            mallGoods.setDetail(goods.getDetail());
            mallGoods.setRetailPrice(goods.getRetailPrice());
            goods.setAddTime(null);
            goods.setUpdateTime(null);
            goods.setDeleted(false);
            mallGoods.setUpdateTime(goods.getUpdateTime());
            mallGoods.setAddTime(goods.getAddTime());
            mallGoods.setDeleted(goods.isDeleted());
            BigDecimal minprice=products.get(0).getPrice();
            for (GoodsCreateProducts product : products) {
                if(minprice.compareTo(product.getPrice()) > -1){
//                    System.out.println("a大于等于b");
                    minprice=product.getPrice();
                }
            }
            mallGoods.setRetailPrice(minprice);
            goodsMapper.insert(mallGoods);

//        part2 specifications
            List<GoodsCreateSpecificationsBO> specifications = goodsCreateRootBO.getSpecifications();

            for (GoodsCreateSpecificationsBO specification : specifications) {
                MallGoodsSpecification mallGoodsSpecification = new MallGoodsSpecification();
                mallGoodsSpecification.setId(null);
                mallGoodsSpecification.setSpecification(specification.getSpecification());
                mallGoodsSpecification.setValue(specification.getValue());
                mallGoodsSpecification.setPicUrl(specification.getPicUrl());
                //记录新知识点 by GJC
                mallGoodsSpecification.setGoodsId(mallGoods.getId());
                specification.setDeleted(false);
                specification.setAddTime(null);
                specification.setUpdateTime(null);
                mallGoodsSpecification.setAddTime(specification.getAddTime());
                mallGoodsSpecification.setUpdateTime(specification.getUpdateTime());
                mallGoodsSpecification.setDeleted(specification.isDeleted());
                specificationMapper.insert(mallGoodsSpecification);
            }

            //part3 products

            for (GoodsCreateProducts product : products) {
                MallGoodsProduct mallGoodsProduct = new MallGoodsProduct();
                mallGoodsProduct.setGoodsId(mallGoods.getId());
                //            ["1.5m床垫*1+枕头*2","玛瑙红"]
                String[] strings = new String[goodsCreateRootBO.getSpecifications().size()];
                for (int j = 0; j < strings.length; j++) {
                    strings[j] = goodsCreateRootBO.getSpecifications().get(j).getValue();
                }
                mallGoodsProduct.setSpecifications(strings);
                mallGoodsProduct.setPrice(product.getPrice());
                mallGoodsProduct.setNumber(product.getNumber());
                mallGoodsProduct.setUrl(product.getUrl());

                product.setDeleted(false);
                product.setAddTime(null);
                product.setUpdateTime(null);
                mallGoodsProduct.setAddTime(product.getAddTime());
                mallGoodsProduct.setUpdateTime(product.getUpdateTime());
                mallGoodsProduct.setDeleted(product.isDeleted());
                productMapper.insert(mallGoodsProduct);
            }
            //part4 attributes
            List<GoodsCreateAttributes> attributes = goodsCreateRootBO.getAttributes();
            for (GoodsCreateAttributes attribute : attributes) {
                MallGoodsAttribute mallGoodsAttribute = new MallGoodsAttribute();
                mallGoodsAttribute.setGoodsId(mallGoods.getId());
                mallGoodsAttribute.setAttribute(attribute.getAttribute());
                mallGoodsAttribute.setValue(attribute.getValue());

                attribute.setDeleted(false);
                attribute.setAddTime(null);
                attribute.setUpdateTime(null);
                mallGoodsAttribute.setAddTime(attribute.getAddTime());
                mallGoodsAttribute.setUpdateTime(attribute.getUpdateTime());
                mallGoodsAttribute.setDeleted(attribute.isDeleted());
                attributeMapper.insert(mallGoodsAttribute);
            }
            return 200;
        } catch (Exception e) {
            return 611;
        }
    }

    //商品管理模块  删除商品功能
    @Override
    public int deleteGood(GoodsDeleteBO goodsDeleteBO) {
//        market_goods表
//        提供条件查询的
        MallGoodsExample mallGoodsExample = new MallGoodsExample();
//        Criteria提供的and方法，就是向条件列表里塞入数据
        MallGoodsExample.Criteria goodsExampleCriteria = mallGoodsExample.createCriteria();

        //条件附加 等于商品id
        goodsExampleCriteria.andIdEqualTo(goodsDeleteBO.getId());
        MallGoods mallGoods = new MallGoods();
        mallGoods.setDeleted(true);
        mallGoods.setUpdateTime(goodsDeleteBO.getUpdateTime());
        //selective动态过滤掉为null的字段
        int i = goodsMapper.updateByExampleSelective(mallGoods, mallGoodsExample);
//      逻辑错误商品删除不能删除大的品牌和种类
////        market_category表
////            提供条件查询
//        MallCategoryExample mallCategoryExample= new MallCategoryExample();
//        MallCategoryExample.Criteria categoryExampleCriteria = mallCategoryExample.createCriteria();
////        条件附加 筛选等于categoryId
//        categoryExampleCriteria.andIdEqualTo(goodsDeleteBO.getCategoryId());
////        修改参数对象
//        MallCategory mallCategory = new MallCategory();
//        mallCategory.setDeleted(true);
//        int j = categoryMapper.updateByExampleSelective(mallCategory, mallCategoryExample);

        // part2 specifications
        MallGoodsSpecificationExample mallGoodsSpecificationExample = new MallGoodsSpecificationExample();
        MallGoodsSpecificationExample.Criteria specificationExampleCriteria = mallGoodsSpecificationExample.createCriteria();
        specificationExampleCriteria.andGoodsIdEqualTo(goodsDeleteBO.getId());
        MallGoodsSpecification mallGoodsSpecification = new MallGoodsSpecification();
        mallGoodsSpecification.setUpdateTime(goodsDeleteBO.getUpdateTime());
        mallGoodsSpecification.setDeleted(true);
        int j = specificationMapper.updateByExampleSelective(mallGoodsSpecification, mallGoodsSpecificationExample);

        //part3
        MallGoodsProductExample mallGoodsProductExample = new MallGoodsProductExample();
        MallGoodsProductExample.Criteria productExampleCriteria = mallGoodsProductExample.createCriteria();
        productExampleCriteria.andGoodsIdEqualTo(goodsDeleteBO.getId());
        MallGoodsProduct mallGoodsProduct = new MallGoodsProduct();
        mallGoodsProduct.setDeleted(true);
        mallGoodsProduct.setUpdateTime(goodsDeleteBO.getUpdateTime());
        int k = productMapper.updateByExampleSelective(mallGoodsProduct, mallGoodsProductExample);

        //part4
        MallGoodsAttributeExample mallGoodsAttributeExample = new MallGoodsAttributeExample();
        MallGoodsAttributeExample.Criteria attributeExampleCriteria = mallGoodsAttributeExample.createCriteria();
        attributeExampleCriteria.andGoodsIdEqualTo(goodsDeleteBO.getId());
        MallGoodsAttribute mallGoodsAttribute = new MallGoodsAttribute();
        mallGoodsAttribute.setDeleted(true);
        mallGoodsAttribute.setUpdateTime(goodsDeleteBO.getUpdateTime());
        int l = attributeMapper.updateByExampleSelective(mallGoodsAttribute, mallGoodsAttributeExample);
        return 200;
    }

    //    商品修改时信息回显  by GJC
    @Override
    public GoodsDetailData getGoodDetail(Integer id) {
        //part1 goods表查
        MallGoodsExample mallGoodsExample = new MallGoodsExample();
        MallGoodsExample.Criteria goodsExampleCriteria = mallGoodsExample.createCriteria();
        //筛选商品id一致
        goodsExampleCriteria.andIdEqualTo(id);
        goodsExampleCriteria.andDeletedEqualTo(false);
        MallGoods goods = goodsMapper.selectByPrimaryKey(id);

        //part2 categoryId

        MallCategoryExample mallCategoryExample = new MallCategoryExample();
        MallCategoryExample.Criteria categoryExampleCriteria = mallCategoryExample.createCriteria();
        categoryExampleCriteria.andIdEqualTo(goods.getCategoryId());
        categoryExampleCriteria.andDeletedEqualTo(false);
        List<MallCategory> category = categoryMapper.selectByExample(mallCategoryExample);
        List<Integer> categoryList = new ArrayList<>();
        for (MallCategory mallCategory : category) {
            categoryList.add(mallCategory.getPid());
            categoryList.add(mallCategory.getId());
        }

//        List<Integer> categoryList= new ArrayList<>();
//        Integer pid = categoryMapper.selectByPrimaryKey(goods.getCategoryId()).getPid();
//        //注意插入顺序
//        categoryList.add(pid);
//        categoryList.add(goods.getCategoryId());

        //part3 product
        MallGoodsProductExample productExample = new MallGoodsProductExample();
        MallGoodsProductExample.Criteria productExampleCriteria = productExample.createCriteria();
        productExampleCriteria.andGoodsIdEqualTo(id);
        productExampleCriteria.andDeletedEqualTo(false);
        List<MallGoodsProduct> productList = productMapper.selectByExample(productExample);

        //part4 attribute
        MallGoodsAttributeExample attributeExample = new MallGoodsAttributeExample();
        MallGoodsAttributeExample.Criteria attributeExampleCriteria = attributeExample.createCriteria();
        attributeExampleCriteria.andGoodsIdEqualTo(id);
        attributeExampleCriteria.andDeletedEqualTo(false);
        List<MallGoodsAttribute> attributeList = attributeMapper.selectByExample(attributeExample);

        //part5 specification
        MallGoodsSpecificationExample specificationExample = new MallGoodsSpecificationExample();
        MallGoodsSpecificationExample.Criteria specificationExampleCriteria = specificationExample.createCriteria();
        specificationExampleCriteria.andDeletedEqualTo(false);
        specificationExampleCriteria.andGoodsIdEqualTo(id);
        List<MallGoodsSpecification> specificationList = specificationMapper.selectByExample(specificationExample);
        return new GoodsDetailData(goods, categoryList, attributeList, specificationList, productList);
    }

    //商品信息修改模块  ByGJC
    @Override
    public int updateGood(GoodsUpdateRoot goodsUpdateRoot) {
        //主体逻辑还是四个表 限制条件修改

        //part1 goods表修改
        try {
            MallGoods goods = goodsUpdateRoot.getGoods();
            MallGoodsExample goodsExample = new MallGoodsExample();
            MallGoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
            goodsExampleCriteria.andIdEqualTo(goods.getId());
            //不能修改已经删除的
            goodsExampleCriteria.andDeletedEqualTo(false);
            goodsMapper.updateByExample(goods, goodsExample);

            //part2 product
            List<MallGoodsProduct> products = goodsUpdateRoot.getProducts();
            for (MallGoodsProduct product : products) {
                MallGoodsProductExample productExample = new MallGoodsProductExample();
                MallGoodsProductExample.Criteria productExampleCriteria = productExample.createCriteria();
                //筛选同条非被删规格的记录
                productExampleCriteria.andDeletedEqualTo(false);
                productExampleCriteria.andIdEqualTo(product.getId());
                productMapper.updateByExample(product, productExample);
            }

            //part3 specification
            List<MallGoodsSpecification> specifications = goodsUpdateRoot.getSpecifications();
            for (MallGoodsSpecification specification : specifications) {
                MallGoodsSpecificationExample specificationExample = new MallGoodsSpecificationExample();
                MallGoodsSpecificationExample.Criteria specificationExampleCriteria = specificationExample.createCriteria();
                specificationExampleCriteria.andDeletedEqualTo(false);
                specificationExampleCriteria.andIdEqualTo(specification.getId());
                specificationMapper.updateByExample(specification, specificationExample);
            }

            //part4 attributes
            List<MallGoodsAttribute> attributes = goodsUpdateRoot.getAttributes();

            for (MallGoodsAttribute attribute : attributes) {
                if (attribute.getId() == null) {
                    attribute.setGoodsId(goods.getId());
                    attribute.setAddTime(new Date());
                    attribute.setUpdateTime(new Date());
                    attribute.setDeleted(false);
                    attributeMapper.insert(attribute);
                } else {

                    MallGoodsAttributeExample attributeExample = new MallGoodsAttributeExample();
                    MallGoodsAttributeExample.Criteria attributeExampleCriteria = attributeExample.createCriteria();
                    attributeExampleCriteria.andDeletedEqualTo(false);
                    attributeExampleCriteria.andIdEqualTo(attribute.getId());
                    attributeMapper.updateByExample(attribute, attributeExample);
                }
            }
            //都修改完成
            return 200;
        } catch (Exception e) {
            //参数出错
            return 611;
        }
    }
}
